import { $, addPage, NamedPage, UserSelectAutoComplete, Notification, delay, i18n, url, request, ConfirmDialog, tpl } from '@hydrooj/ui-default'

addPage(new NamedPage(['修改用户名'], () => {
    UserSelectAutoComplete.getOrConstruct($('[name="uidOrName"]'), {
        clearDefaultValue: false,
    });
}));

addPage (new NamedPage('修改用户名', () => {
  $(document).on('click', '[type="submit"]', async (ev) => {
    ev.preventDefault();

    const $form = $(ev.currentTarget).closest('form');
    try {
      const res = await request.post('', {
        password: $form.find('[name="password"]').val(),
        uidOrName: $form.find('[name="uidOrName"]').val(),
        newUname: $form.find('[name="newUname"]').val(),
      });
      if (res.success) {
        Notification.success(i18n('修改用户名成功!'));
        await delay(1000);
        window.location.reload();  
      }
    } catch (e) {
      Notification.error(e.message);
    }
  });
}));

addPage(new NamedPage('消息管理', () => {
  $(document).on('click', '[name="operation"][value="delete"]', async (ev) => {
    ev.preventDefault();
    const message = '真的要删除这条消息吗？';
    const action = await new ConfirmDialog({
        $body: tpl`
            <div class="typo">
                <p>${i18n(message)}</p>
            </div>`,
    }).open();
    if (action !== 'yes') return;

    const $form = $(ev.currentTarget).closest('form'); 
    try {
        const res = await request.post('', {
          operation: 'delete',
          id: $form.find('[name="id"]').val(),
        });
      if (res.success) {
        Notification.success(i18n('删除消息成功！'));
        await delay(1000);
        window.location.reload();
      }
    } catch (e) {
        Notification.error(e.message);
    }
  });
}));

addPage (new NamedPage('批量设置用户权限', () => {
  async function post(draft) {
    try {
      const res = await request.post('', {
        users: $('[name="users"]').val(),
        draft,
      });
      if (!draft) {
        Notification.success(i18n('Updated {0} user records.', res.users.length));
        await delay(2000);
        window.location.reload();
      } else {
        $('[name="messages"]').text(res.messages.join('\n'));
      }
    } catch (e) {
      Notification.error(e.message);
    }
  }

  $('[name="preview"]').on('click', () => post(true));
  $('[name="submit"]').on('click', () => post(false));
}));

addPage(new NamedPage('用户管理', () => {
  $(document).on('click', '[type="submit"]', async (ev) => {
    ev.preventDefault();
    const $form = $(ev.currentTarget).closest('form'); 
    const uname = $form.find('[name="uname"]').val();
    const message = `确定要封禁用户 ${uname} ？`;
    const action = await new ConfirmDialog({
        $body: tpl`
            <div class="typo">
                <p>${i18n(message)}</p>
            </div>`,
    }).open();
    if (action !== 'yes') return;

    try {
        const res = await request.post('', {
          uid: $form.find('[name="uid"]').val(),
        });
      if (res.success) {
        Notification.success(i18n('该用户已被封禁！'));
        await delay(1000);
        window.location.reload();
      }
    } catch (e) {
        Notification.error(e.message);
    }
  });
}));
